package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.DataList;
import beans.HttpConfig;
import net.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DataListServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		 String dir = request.getParameter("layer");
			 if(dir ==null){
				 dir = "";
			 }
			String url = HttpConfig.filePath + dir;
			String param = "op=LISTSTATUS";
			
			JSONObject result = JSONObject.fromObject(HttpRequest.sendGet(url, param));
			
			JSONArray datas = new JSONArray();
			List<Object> results = new ArrayList();
			datas = result.getJSONObject("FileStatuses").getJSONArray("FileStatus");
			for(int i=0;i<datas.size();i++){
				JSONObject o = (JSONObject) datas.get(i);
				DataList data = new DataList();
				data.setFileLength(o.getString("length"));
				data.setModifyTime(o.getString("modificationTime"));
				data.setPathSuffix(o.getString("pathSuffix"));
				data.setPermission(o.getString("permission"));
				data.setType(o.getString("type"));
				
				results.add(data);
			}
			
			JSONObject json = new JSONObject();
			json.put("data", results);
			response.getWriter().print(json.toString());
	 }

	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        doPost(request,response);
	    }
}
